package com.xj.opcua.client.util;

import java.util.UUID;

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.nodes.UaNode;
import org.eclipse.milo.opcua.stack.core.UaException;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
import org.eclipse.milo.opcua.stack.core.types.enumerated.IdType;

public class OpcUaUtils {
	
	public static boolean isBaseObject(Object obj) {
		String className = obj.getClass().getSimpleName();
		if("Byte".equals(className) 
				|| "Short".equals(className)
				||"UShort".equals(className)
				||"Integer".equals(className)
				||"UInteger".equals(className)
				||"Long".equals(className)
				||"Float".equals(className)
				||"Double".equals(className)
				||"Character".equals(className)
				||"Boolean".equals(className)
				||"String".equals(className)
				||"ByteString".equals(className)
				||"UByte".equals(className)
				||"LocalizedText".equals(className)
				||"DateTime".equals(className)
				||"NodeId".equals(className)
				) {
			return true;
		}
		return false;
	}
	
	public static NodeId getNodeId(int namespaceIndex,Object identifier) {
		NodeId nodeId;
		if (identifier instanceof UInteger) {
			nodeId = new NodeId(namespaceIndex, (UInteger) identifier);
        } else if (identifier instanceof Integer) {
        	nodeId = new NodeId(namespaceIndex, (Integer)identifier);
        } else if (identifier instanceof String) {
        	nodeId = new NodeId(namespaceIndex, (String)identifier);
        } else if (identifier instanceof UUID) {
        	nodeId = new NodeId(namespaceIndex, (UUID)identifier);
        } else {
        	nodeId = new NodeId(namespaceIndex, (String)identifier.toString());
        }
		return nodeId;
	}

	
	public static IdType getType(Object identifier) {
        if (identifier instanceof UInteger) {
            return IdType.Numeric;
        } else if (identifier instanceof String) {
            return IdType.String;
        } else if (identifier instanceof UUID) {
            return IdType.Guid;
        } else {
            return IdType.Opaque;
        }
    }
	
	public static String toParseableString(Object identifier) {
		String type = "s";
        switch (getType(identifier)) {
            case Numeric:
            	type = "i";
                break;
            case String:
                type = "s";
                break;
            case Guid:
                type = "g";
                break;
            case Opaque:
                type = "b";
                break;
            default:
                throw new IllegalStateException("unknown IdType: " + identifier);
        }
        return type;
    }
	
	public static String getUaClientClass(OpcUaClient client,NodeId nodeId) {
		UaNode uaNode;
		String className = "";
		try {
			uaNode = client.getAddressSpace().getNode(nodeId);
			className = uaNode.getClass().getSimpleName();
		} catch (UaException e) {
			//logger.error("getUaClientClass:",e);
		}
		return className;
	}
	
	public static boolean isBaseClass(String className) {
		if(className.equals(UaClientEnum.BaseDataVariableTypeNode.code) 
    			|| className.equals(UaClientEnum.PropertyTypeNode.code)
    			|| className.equals(UaClientEnum.BaseVariableTypeNode.code))
		return true;
		else return false;
	}
}
